
var MY_MEDICAL_HIS = (function() {
    var mainColor = 'rgb(2, 251, 255)';

    //获取页面统计信息
    function loadChartInfo() {
        $.ajax({
            type: "POST",
            url: "invs_home_page.do/load_chart_info",
            data: {id : '22', datePatten : 1},
            success: function(data) {
                WeKnow.closeLoading();
                if (!WeKnow.filterAjaxResult(data)) return false; // session过期check

                if (data.errorcode == 0) {
                    //电子病率覆盖率
                    drawElEctronicMedicalHisPie(data.chartInfo.caseCoverage);
                    //卫生台账覆盖率
                    drawHealthAccountPie(data.chartInfo.accountCoverage);
                    //处方违规数
                    drawPrescriptionViolationPolar(data.chartInfo.prescriptionViolation);
                    //卫生台账类型
                    drawHealthAccountBar(data.chartInfo.accountType);
                    //处方违规数趋势
                    drawPrescriptionViolationTrendLine(data.chartInfo.violateTimeLine);
                    //台账异常数趋势
                    drawAbnormalAccountTrendLine(data.chartInfo.warningTimeLine);
                    //电子病历机构数量
                    drawMedicalTotal(data.chartInfo.allCaseCnt);
                    //电子处方机构数量
                    drawPrescriptionTotal(data.chartInfo.allPresCnt);
                    //卫生台账机构数量
                    drawAccountTotal(data.chartInfo.allActionCnt);
                    //诊疗行为违规机构
                    drawPrescriptionViolationProportion(data.chartInfo.illegalInstitution);
                    //台账异常机构
                    drawAbnormalAccountProportion(data.chartInfo.abnormalInstitution);
                } else if (data.errorcode == -1 ){
                    WeKnow.errorMsg( data.errMsg );
                } else {
                    WeKnow.systemMsg('查询失败，请重新登录');
                }
            }
        });
    }

    //诊疗行为违规机构
    function drawPrescriptionViolationProportion(data) {
        if(data == null || data.length == 0) {
            return;
        }

        //清空
        document.getElementById("prescriptionViolationProportion").innerHTML = "";

        var percent = data.all > 0 ? Math.round(data.part  * 100 / data.all) : 0;

        $("#prescriptionViolationProportion").append(
            "<span>" + data.part + "家</span>"
            + "<span>" + percent +"%</span>"
        );
    }

    //台账异常机构
    function drawAbnormalAccountProportion(data) {
        if(data == null || data.length == 0) {
            return;
        }

        //清空
        document.getElementById("abnormalAccountProportion").innerHTML = "";

        var percent = data.all > 0 ? Math.round(data.part * 100 / data.all) : 0;

        $("#abnormalAccountProportion").append(
            "<span>" + data.part + "家</span>"
            + "<span>" + percent +"%</span>"
        );
    }

    //电子病历机构数量
    function drawMedicalTotal(data) {
        if(data == null || data.length == 0) {
            return;
        }

        //清空
        document.getElementById("medicalTotal").innerHTML = "";

        var numbers =  data.toString();
        var length = numbers.length;

        for(var i=0; i < 4-length; i++) {
            $("#medicalTotal").append(
                "<span class='number'>" + 0 + "</span>"
            );
        }

        for(var i=0; i < length; i++) {
            $("#medicalTotal").append(
                "<span class='number'>" + numbers[i] + "</span>"
            );
        }
    }

    //电子处方机构数量
    function drawPrescriptionTotal(data) {
        if(data == null || data.length == 0) {
            return;
        }

        //清空
        document.getElementById("prescriptionTotal").innerHTML = "";

        var numbers =  data.toString();
        var length = numbers.length;

        for(var i=0; i < 4-length; i++) {
            $("#prescriptionTotal").append(
                "<span class='number'>" + 0 + "</span>"
            );
        }

        for(var i=0; i < length; i++) {
            $("#prescriptionTotal").append(
                "<span class='number'>" + numbers[i] + "</span>"
            );
        }
    }

    //卫生台账机构数量
    function drawAccountTotal(data) {
        if(data == null || data.length == 0) {
            return;
        }

        //清空
        document.getElementById("accountTotal").innerHTML = "";

        var numbers =  data.toString();
        var length = numbers.length;

        for(var i=0; i < 5-length; i++) {
            $("#accountTotal").append(
                "<span class='number'>" + 0 + "</span>"
            );
        }

        for(var i=0; i < length; i++) {
            $("#accountTotal").append(
                "<span class='number'>" + numbers[i] + "</span>"
            );
        }
    }

    //电子病率覆盖率
    function drawElEctronicMedicalHisPie(data) {
        if (data.length == 0) {
            return;
        }

        // 1. 实例化对象
        var myChart = echarts.init(document.querySelector("#electronicMedicalHisPie"));

        // 2. 指定配置项和数据
        var dataStyle = {
            normal: {
                label: {show:false},
                labelLine: {show:false}
            }
        };

        var placeHolderStyle = {
            normal : {
                color: 'rgba(165,204,237,0.1)',
                label: {show:false},
                labelLine: {show:false}
            }
        };

        var series=[];
        var nameData = [];
        for(var i=0;i<data.length;i++){
            nameData.push(data[i].name);
            var name = data[i].name;
            var value = data[i].value;
            var radius1 = fontChart(1.375) - i*fontChart(0.125);
            var radius2 = fontChart(1.4875) - i*fontChart(0.125);
            series.push({
                name:'',
                type:'pie',
                clockWise:false,
                hoverAnimation: false,
                radius : [radius1, radius2],
                itemStyle : dataStyle,
                data:[
                    {
                        value: value,
                        name: name
                    },
                    {
                        value: 100 - value,
                        name: name,
                        itemStyle : placeHolderStyle,
                    }
                ]
            })
        }

        option = {
            title: {
                text: "电子病历",
                subtext: '·覆盖率',
                x: 'center',
                y: 'center',
                itemGap: fontChart(0.125),//主副标题纵向间隔
                textStyle : {
                    color: '#fff',
                    fontSize: fontChart(0.225),
                    fontWeight: 'normal'
                },
                subtextStyle: {
                    color: '#fff',
                    fontSize: fontChart(0.225),
                },
            },
            tooltip : {
                show: true,
                formatter: "{b}"
            },
            color: [
                '#00A0E9',
                '#01FFFF',
                '#3BB568',
                '#FDF200',
                '#F5A045',
                '#F65F70',
            ],
            legend: {
                icon: 'rect',
                itemHeight: fontChart(0.125),
                itemWidth: fontChart(0.1875),
                orient : 'vertical',
                y : fontChart(0.5625),
                right: '1%',
                itemGap: fontChart(0.0625),
                data: nameData,
                textStyle: {
                    color: '#fff'
                }
            },
            series : series,
        };

        // 3. 配置项和数据给我们的实例化对象
        myChart.setOption(option);
        // 4. 当我们浏览器缩放的时候，图表也等比例缩放
        window.addEventListener("resize", function() {
            // 让我们的图表调用 resize这个方法
            myChart.resize();
        });
    }

    //卫生台账覆盖率
    function drawHealthAccountPie(data) {
        if (data.length == 0) {
            return;
        }

        // 1. 实例化对象
        var myChart = echarts.init(document.querySelector("#healthAccountPie"));

        // 2. 指定配置项和数据
        var dataStyle = {
            normal: {
                label: {show:false},
                labelLine: {show:false}
            }
        };

        var placeHolderStyle = {
            normal : {
                color: 'rgba(165,204,237,0.1)',
                label: {show:false},
                labelLine: {show:false}
            },
        };

        var series=[];
        var nameData = [];
        for(var i=0;i<data.length;i++){
            nameData.push(data[i].name);
            var name = data[i].name;
            var value = data[i].value;
            var radius1 = fontChart(1.375) - i*fontChart(0.125);
            var radius2 = fontChart(1.4875) - i*fontChart(0.125);
            series.push({
                name:'',
                type:'pie',
                clockWise:false,
                hoverAnimation: false,
                radius : [radius1, radius2],
                itemStyle : dataStyle,
                data:[
                    {
                        value: value,
                        name: name,
                    },
                    {
                        value: 100 - value,
                        name: name,
                        itemStyle : placeHolderStyle
                    }
                ]
            })
        }

        option = {
            title: {
                text: "卫生台账",
                subtext: '·覆盖率',
                x: 'center',
                y: 'center',
                itemGap: fontChart(0.125),//主副标题纵向间隔
                textStyle : {
                    color: '#fff',
                    fontSize: fontChart(0.225),
                    fontWeight: 'normal'
                },
                subtextStyle: {
                    color: '#fff',
                    fontSize: fontChart(0.225),
                },
            },
            tooltip : {
                show: true,
                formatter: "{b}"
            },
            color: [
                '#00A0E9',
                '#01FFFF',
                '#3BB568',
                '#FDF200',
                '#F5A045',
                '#F65F70',
            ],
            legend: {
                icon: 'rect',
                itemHeight: fontChart(0.125),
                itemWidth: fontChart(0.1875),
                orient : 'vertical',
                y : fontChart(0.5625),
                right: '1%',
                itemGap: fontChart(0.0625),
                data: nameData,
                textStyle: {
                    color: '#fff'
                }
            },
            series : series,
        };

        // 3. 配置项和数据给我们的实例化对象
        myChart.setOption(option);
        // 4. 当我们浏览器缩放的时候，图表也等比例缩放
        window.addEventListener("resize", function() {
            // 让我们的图表调用 resize这个方法
            myChart.resize();
        });
    }

    //处方违规数
    function drawPrescriptionViolationPolar(data) {
        if (data.length == 0) {
            return;
        } else {
            var nameData = [];
            for(i=0;i<data.length;i++){
                nameData.push(data[i].name);
            }
        }
        // 1. 实例化对象
        var myChart = echarts.init(document.querySelector("#prescriptionViolationPolar"));
        // 2. 指定配置项和数据
        option = {
            title: {
                text: "处方违规数",
                x: 'center',
                textStyle : {
                    color: '#fff',
                    fontSize: fontChart(0.225),
                    fontWeight: 'normal'
                },
            },
            tooltip: {
                trigger: 'item',
            },
            polar: {
                center: ['50%', '55%']
            },
            angleAxis: {//极坐标系的角度轴
                type: 'category',
                startAngle: 90,
                splitNumber: 12, //坐标轴分割段数
                axisLine: { //坐标轴轴线设置
                    lineStyle: {
                        color: 'rgba(255, 255, 255, 0.6)',
                    }
                },
                axisTick: { //坐标轴刻度设置
                    show: true,
                },
                splitLine: { //分割线设置
                    show: false,
                },
                axisLabel: { //刻度标签设置
                    textStyle: {
                        color: 'rgb(255, 204, 86)'
                    }
                },
                data: nameData
            },
            radiusAxis: {
                min: 0,
                minInterval: 1,
                axisLine: { //坐标轴轴线设置
                    show: true,
                    lineStyle: {
                        color: 'rgba(255, 255, 255, 0.6)'
                    }
                },
                axisTick: { //坐标轴刻度设置
                    show: true
                },
                axisLabel: { //刻度标签设置
                    margin: 5, //刻度与坐标轴之间的距离
                    textStyle: {
                        color: 'rgb(255, 204, 86)'
                    }
                }
            },
            series: [{
                center: ['50%', '55%'],
                type: 'bar',
                data: data,
                coordinateSystem: 'polar',
                lineStyle: {
                    normal: {
                        color: 'rgba(255, 255, 255, 0.9)'
                    }
                },
                itemStyle: {
                    normal: {
                        color: '#FFCC56'
                    }
                },
                animationDuration: 2000
            }],
        };

        // 3. 配置项和数据给我们的实例化对象
        myChart.setOption(option);
        // 4. 当我们浏览器缩放的时候，图表也等比例缩放
        window.addEventListener("resize", function() {
            // 让我们的图表调用 resize这个方法
            myChart.resize();
        });
    }

    //卫生台账类型
    function drawHealthAccountBar(data) {
        if (data.length == 0) {
            return;
        } else {
            var nameData = [];
            var valueData = [
                {"name":"器械消毒", "value": 0},{"name":"场所消毒", "value": 0},{"name":"医疗废物", "value": 0},
                {"name":"污水余氯", "value": 0},{"name":"污水检测", "value": 0},{"name":"器械灭菌检测", "value": 0}
            ];
            for(var i=0; i < valueData.length; i++){
                nameData.push(valueData[i].name);
                switch (i) {
                    case 0:
                        valueData[i].value = data.t101;
                        break;
                    case 1:
                        valueData[i].value = data.t102;
                        break;
                    case 2:
                        valueData[i].value = data.t201;
                        break;
                    case 3:
                        valueData[i].value = data.t302;
                        break;
                    case 4:
                        valueData[i].value = data.t301;
                        break;
                    case 5:
                        valueData[i].value = data.t303;
                        break;
                }
            }
        }
        // 1. 实例化对象
        var myChart = echarts.init(document.querySelector("#healthAccountBar"));
        // 2. 指定配置项和数据
        option = {
            title: {
                // text: "卫生台账类型",
                subtext: "累计台账数",
                left: 'top',
                textStyle : {
                    color: '#fff',
                    fontSize: fontChart(0.225),
                    fontWeight: 'normal',
                    textAlign: 'center'
                },
                subtextStyle: {//副标题的属性
                    color: mainColor,
                    fontSize: fontChart(0.15),
                },
            },
            grid:{
                x: 40,
                y: 60,
                x2: 15,
                y2: 60,
            },
            tooltip : {
                trigger: 'item'
            },
            calculable : true,
            xAxis : [
                {
                    type : 'category',
                    data : nameData,
                    offset: 5,
                    axisLine: {
                        lineStyle: {
                            color: '#02FBFF'
                        }
                    },
                    splitLine: {
                        show: false
                    },
                    axisTick: {
                        show: false,
                        alignWithLabel: true
                    },
                    axisLabel: {
                        interval:0,
                        rotate:20,
                    }
                }
            ],
            yAxis : [
                {
                    type : 'value',
                    minInterval : 1,
                    axisLine: {
                        lineStyle: {
                            color: '#02FBFF'
                        }
                    },
                    splitLine: {
                        show: false
                    },
                    axisTick: {
                        show: false
                    }
                }
            ],
            series : [
                {
                    type:'bar',
                    data:valueData,
                    itemStyle: {
                        normal: {
                            color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [{
                                offset: 0,
                                color: '#02FBFF'
                            }, {
                                offset: 1,
                                color: '#195bff'
                            }]),
                        }
                    },
                    barWidth : fontChart(0.375)
                }
            ]
        };

        // 3. 配置项和数据给我们的实例化对象
        myChart.setOption(option);
        // 4. 当我们浏览器缩放的时候，图表也等比例缩放
        window.addEventListener("resize", function() {
            // 让我们的图表调用 resize这个方法
            myChart.resize();
        });
    };

    //处方违规数趋势
    function drawPrescriptionViolationTrendLine(data) {
        if (data.length == 0) {
            return;
        } else {
            var nameData = [];
            var valueData = [];
            var year = (new Date()).getFullYear();
            var cWeek = data.length > 0 ? data[0].name : '';
            for(var i=data.length; i > 0; i--){
                var temp = cWeek >= data[i-1].name ? year : year-1;
                data[i-1].name = temp.toString().substring(2,4) + '年' + data[i-1].name + '周';
                nameData.push(data[i-1].name);
                valueData.push(data[i-1]);
            }
        }
        // 基于准备好的dom，初始化echarts实例
        var myChart = echarts.init(document.querySelector("#prescriptionViolationTrendLine"));
        // 2. 指定配置和数据
        option = {
            title: {
                text: "处方违规数趋势",
                x: 'center',
                top: '5%',
                textStyle : {
                    color: '#fff',
                    fontSize: fontChart(0.225),
                    fontWeight: 'normal'
                },
            },
            tooltip: {
                trigger: "axis",
                axisPointer: {
                    lineStyle: {
                        color: mainColor
                    }
                }
            },
            grid: {
                left: fontChart(0.375),
                right: fontChart(0.25),
                bottom: fontChart(0.1875),
                containLabel: true
            },
            xAxis: [
                {
                    type: "category",
                    boundaryGap: false,
                    axisLine: {
                        lineStyle: {
                            color: mainColor,
                        },
                    },
                    axisTick: {
                        show: false
                    },
                    splitLine: {
                        show: false
                    },
                    data: nameData,
                    axisLabel: {
                        margin: fontChart(0.2),
                        interval:0, //坐标刻度之间的显示间隔，默认就可以了（默认是不重叠）
                        rotate: 30,   //调整数值改变倾斜的幅度（范围-90到90）
                        textStyle: {
                            color: mainColor,
                            fontSize: fontChart(0.15)
                        }
                    },
                    grid: {
                        left: '0%',
                        bottom:'90%'
                    },
                },
                {
                    axisPointer: { show: false },
                    axisLine: { show: false },
                    position: "bottom",
                    offset: 20
                }
            ],
            yAxis: [
                {
                    type: "value",
                    min: 0,
                    minInterval: 1,
                    axisTick: { show: false },
                    axisLine: {
                        lineStyle: {
                            color: '#2898e5',
                        },
                    },
                    axisLabel: {
                        textStyle: {
                            color: mainColor,
                            fontSize: fontChart(0.15)
                        }
                    },
                    splitLine: {
                        show: false
                    }
                }
            ],
            series: [
                {
                    name: "",
                    type: "line",
                    smooth: true,
                    symbol: "circle",
                    symbolSize: 5,
                    showSymbol: false,
                    lineStyle: {
                        normal: {
                            color: mainColor,
                            width: 2
                        }
                    },
                    areaStyle: {
                        normal: {
                            color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [{
                                offset: 0,
                                color: mainColor
                            }, {
                                offset: 1,
                                color: 'rgba(88,255,225,0.1)'
                            }]),
                        }
                    },
                    itemStyle: {
                        normal: {
                            color: mainColor,
                            borderColor: "rgba(221, 220, 107, .1)",
                            borderWidth: 12
                        }
                    },
                    data: valueData
                }
            ]
        };
        // 3. 把配置和数据给实例对象
        myChart.setOption(option);

        // 重新把配置好的新数据给实例对象
        myChart.setOption(option);
        window.addEventListener("resize", function() {
            myChart.resize();
        });
    }

    //台账异常数趋势
    function drawAbnormalAccountTrendLine(data) {
        if (data.length == 0) {
            return;
        } else {
            var nameData = [];
            var valueData = [];
            var year = (new Date()).getFullYear();
            var cWeek = data.length > 0 ? data[0].name : '';
            for(var i=data.length; i > 0; i--){
                var temp = cWeek >= data[i-1].name ? year : year-1;
                data[i-1].name = temp.toString().substring(2,4) + '年' + data[i-1].name + '周';
                nameData.push(data[i-1].name);
                valueData.push(data[i-1]);
            }
        }
        // 基于准备好的dom，初始化echarts实例
        var myChart = echarts.init(document.querySelector("#abnormalAccountTrendLine"));
        // 2. 指定配置和数据
        option = {
            title: {
                text: "台账异常数趋势",
                x: 'center',
                top: '5%',
                textStyle : {
                    color: '#fff',
                    fontSize: fontChart(0.225),
                    fontWeight: 'normal'
                },
            },
            tooltip: {
                trigger: "axis",
                axisPointer: {
                    lineStyle: {
                        color: mainColor
                    }
                }
            },
            grid: {
                left: fontChart(0.375),
                right: fontChart(0.25),
                bottom: fontChart(0.1875),
                containLabel: true
            },
            xAxis: [
                {
                    type: "category",
                    boundaryGap: false,
                    axisLine: {
                        lineStyle: {
                            color: mainColor,
                        },
                    },
                    axisTick: {
                        show: false
                    },
                    splitLine: {
                        show: false
                    },
                    data: nameData,
                    axisLabel: {
                        margin: fontChart(0.2),
                        interval:0, //坐标刻度之间的显示间隔，默认就可以了（默认是不重叠）
                        rotate: 30,   //调整数值改变倾斜的幅度（范围-90到90）
                        textStyle: {
                            color: mainColor,
                            fontSize: fontChart(0.15)
                        }
                    },
                    grid: {
                        left: '0%',
                        bottom:'90%'
                    },
                },
                {
                    axisPointer: { show: false },
                    axisLine: { show: false },
                    position: "bottom",
                    offset: 20
                }
            ],
            yAxis: [
                {
                    type: "value",
                    min: 0,
                    minInterval: 1,
                    axisTick: { show: false },
                    axisLine: {
                        lineStyle: {
                            color: '#2898e5',
                        },
                    },
                    axisLabel: {
                        textStyle: {
                            color: mainColor,
                            fontSize: fontChart(0.15)
                        }
                    },
                    splitLine: {
                        show: false
                    }
                }
            ],
            series: [
                {
                    name: "",
                    type: "line",
                    smooth: true,
                    symbol: "circle",
                    symbolSize: 5,
                    showSymbol: false,
                    lineStyle: {
                        normal: {
                            color: mainColor,
                            width: 2
                        }
                    },
                    areaStyle: {
                        normal: {
                            color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [{
                                offset: 0,
                                color: mainColor
                            }, {
                                offset: 1,
                                color: 'rgba(88,255,225,0.1)'
                            }]),
                        }
                    },
                    itemStyle: {
                        normal: {
                            color: mainColor,
                            borderColor: "rgba(221, 220, 107, .1)",
                            borderWidth: 12
                        }
                    },
                    data: valueData
                }
            ]
        };
        // 3. 把配置和数据给实例对象
        myChart.setOption(option);

        // 重新把配置好的新数据给实例对象
        myChart.setOption(option);
        window.addEventListener("resize", function() {
            myChart.resize();
        });
    }

    //自适应字体大小
    function fontChart(res) {
        //获取到屏幕的宽度
        var clientWidth =
            window.innerWidth ||
            document.documentElement.clientWidth ||
            document.body.clientWidth;
        if (!clientWidth) return; //报错拦截：
        var fontSize = 80 * (clientWidth / screen.width);
        return res * fontSize;
    }

    var elEctronicMedicalHisPieData = [
        {'name':'湖滨街道75%', 'value':75},{'name':'清波街道70%', 'value':70},{'name':'小营街道60%', 'value':60},
        {'name':'望江街道60%', 'value':60},{'name':'南星街道45%', 'value':45},{'name':'紫阳街道20%', 'value':20},
    ];

    var healthAccountPieData = [
        {'name':'口腔', 'value':130},{'name':'医美', 'value':130},{'name':'中医', 'value':60},{'name':'西医', 'value':160},{'name':'中西医', 'value':20}
    ];

    var prescriptionViolationPolarData = [
        {'name':'湖滨街道', 'value':120},{'name':'清波街道', 'value':100},
        {'name':'小营街道', 'value':160},{'name':'望江街道', 'value':120},
        {'name':'南星街道', 'value':180},{'name':'紫阳街道', 'value':100}
    ];

    var healthAccountBarData = [
        {'name':'场所消毒', 'value':20},{'name':'器械消毒', 'value':70},{'name':'医疗废物', 'value':70},
        {'name':'污水余氯', 'value':130},{'name':'污水检测', 'value':20},{'name':'器械灭菌检测', 'value':80}
    ];

    var prescriptionViolationTrendLineData = [
        {'name':'4月第1周', 'value':120},{'name':'4月第2周', 'value':170},{'name':'4月第3周', 'value':140},
        {'name':'4月第4周', 'value':100},{'name':'4月第5周', 'value':80},{'name':'4月第6周', 'value':200},
        {'name':'4月第7周', 'value':70}
    ];

    var abnormalAccountTrendLineData = [
        {'name':'4月第1周', 'value':120},{'name':'4月第2周', 'value':170},{'name':'4月第3周', 'value':140},
        {'name':'4月第4周', 'value':100},{'name':'4月第5周', 'value':80},{'name':'4月第6周', 'value':200},
        {'name':'4月第7周', 'value':70}
    ];

    return {
        loadChartInfo:loadChartInfo
    }
})();

$(document).ready(function() {

    MY_MEDICAL_HIS.loadChartInfo();
    setInterval(function(){ MY_MEDICAL_HIS.loadChartInfo(); }, 5000);

});